package com.gx.smart.building.ui.inspection.fragment

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.blankj.utilcode.util.ToastUtils
import com.gx.smart.base.BaseMVVMFragmentV2
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentInspectionTransformBinding
import com.gx.smart.building.ui.inspection.InspectionTransformResultActivity
import com.gx.smart.building.ui.inspection.viewmodel.InspectionViewModel
import com.gx.smart.building.ui.repair.ProcessingTransformSearchActivity

class InspectionTransformFragment : BaseMVVMFragmentV2<InspectionViewModel>() {
    private var name: String? = null
    private var id: String? = null

    private lateinit var viewBinding: FragmentInspectionTransformBinding

    override fun onBindLayout(): Int = R.layout.fragment_inspection_transform

    override fun getToolbarTitle(): String = "转派"

    override fun onBindViewModel(): Class<InspectionViewModel> = InspectionViewModel::class.java

    override fun initContentView(root: ViewGroup) {
        viewBinding = FragmentInspectionTransformBinding.inflate(layoutInflater, root, true)
    }


    override fun initContent() {
        super.initContent()
        viewBinding.contentEdit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                viewBinding.wordCount.text = s.length.toString() + "/200"
            }
        })

        viewBinding.save.setOnClickListener {
            save()
        }
        viewBinding.nameLayout.setOnClickListener {
            val intent = Intent(requireActivity(), ProcessingTransformSearchActivity::class.java)
            searchLauncher.launch(intent)
        }
    }

    //选择房间跳转页面
    private val searchLauncher: ActivityResultLauncher<Intent> =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            val resultCode: Int = result.resultCode
            if (resultCode == Activity.RESULT_OK) {
                val data: Intent? = result.data
                if (data != null) {
                    name = data.getStringExtra("name")
                    id = data.getStringExtra("id")
                    viewBinding.userName.text = name
                }
            }
        }


    private fun save() {
        val suggestion = viewBinding.contentEdit.text.toString().trim()
        if (suggestion.isNullOrEmpty()) {
            ToastUtils.showLong("请填写处理意见")
            return
        }

        if (name.isNullOrEmpty()) {
            ToastUtils.showLong("请选择处理人")
            return
        }

        viewModel.forwardTask(
            requireActivity().intent.getStringExtra("id")!!,
            suggestion,
            name!!,
            id!!
        )
            .observe(viewLifecycleOwner) {
                if (it) {
                    startActivity(
                        Intent(
                            requireActivity(),
                            InspectionTransformResultActivity::class.java
                        )
                    )
                    requireActivity().finish()
                }
            }
    }
}